package com.zhao.system.Controller;

import com.zhao.system.poro.User;
import com.zhao.system.poro.UserInfo;
import com.zhao.system.service.UserInfoService;
import com.zhao.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import response.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/userInfo")
@Transactional
@CrossOrigin(allowCredentials ="true")
public class UserInfoController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/add")
    public Result add(@RequestBody UserInfo userInfo,HttpServletRequest request){
        HttpSession session = request.getSession();
        int id = (int) session.getAttribute("id");
        userInfo.setUid(id);
        userInfoService.add(userInfo);
        return Result.ok();
    }
    @RequestMapping("/find")
    public Result find(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        UserInfo info = userInfoService.find(user.getId());
        return Result.ok().data("info",info);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody UserInfo userInfo){
        System.out.println(userInfo);
        int i = userInfoService.update(userInfo);
        return Result.ok();
    }
}
